package com.yxhl.zoume.core.func.webpage.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.func.webpage.activity.WebPageActivity;
import com.yxhl.zoume.core.func.webpage.info.WebPageEntrance;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import butterknife.BindView;

/**
 * Created by vhawk on 16/4/12.
 */
public class HTMLFragment extends BaseFragment {
    private static final String BUNDLE_EXTRA_PARAM = "bundle_extra_param";

    @BindView(R.id.tv_content)
    TextView contentTv;

    private WebPageEntrance mEntrance;

    public static Fragment newInstance(WebPageEntrance type) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(BUNDLE_EXTRA_PARAM, type);
        HTMLFragment fragment = new HTMLFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        handleArguments();
        View view = super.onCreateView(inflater, container, savedInstanceState);
        initLocalIntroduction();
        return view;
    }

    private void handleArguments() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mEntrance = (WebPageEntrance) arguments.getSerializable(BUNDLE_EXTRA_PARAM);
        }
    }

    /**
     * 加载指定本地页面
     */
    private void initLocalIntroduction() {
        if (null != mEntrance) {
            InputStream is = this.getResources().openRawResource(mEntrance.getContentRes());
            try {
                DataInputStream dis = new DataInputStream(is);
                byte[] buffer = new byte[is.available()];
                dis.readFully(buffer);
                contentTv.setText(Html.fromHtml(new String(buffer)));
                dis.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void initializeInjector() {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_func_local_introduction;
    }

    @Override
    protected void setupToolBar() {
        int titleRes = 0;
        switch (mEntrance) {
            case TICKETING_INFORMATION:
                titleRes = R.string.title_ticketing_information;
                break;
            case ZOU_ME_BUS_INTRODUCTION:
                titleRes = R.string.zoume_bus_header_title;
                break;
            case REGISTER_AGREEMENT:
                titleRes = R.string.register_agreement;
                break;
            case FAQ:
                titleRes = R.string.common_problem;
                break;
            case PAYMENT_INTRODUCTION:
                titleRes = R.string.pay_desc;
                break;

        }

        if (mActivity instanceof WebPageActivity) {
            ((WebPageActivity) mActivity).setToolbarTitle(getString(titleRes));
        }
    }
}
